	//规则, 组件名字驼峰, 元素标签中划线分割, 两者映射缺一不可.
angular.module('app').component('winitHistoryFeatureMap', {
	templateUrl: getTemplateUrl('matrix/component/tpl/winitHistoryFeatureMap.html'),
		bindings: {
			blur  : '&',
			datas : '<',
		},
		controller: function ($scope, $element, $attrs){
			var ctrl = this;
			console.log(ctrl)

				var $ = go.GraphObject.make;
				myHistory =
			    $(go.Diagram, "myHistoryDiv",
			      {
			        initialContentAlignment: go.Spot.MiddleTop,
			        padding: 10,
			        isReadOnly: true,
			        layout: $(go.LayeredDigraphLayout,
			                  { direction: 90, layeringOption: go.LayeredDigraphLayout.LayerLongestPathSource }),
			        "undoManager.isEnabled": true
			      });

				myHistory.nodeTemplate =
					$(go.Node, go.Panel.Vertical,
			      { portId: "", fromLinkable: true, toLinkable: true },
			      new go.Binding("visible"),
						$(go.Panel, go.Panel.Auto,
							$(go.Shape,
			          { fill: "white", minSize: new go.Size(30, 30), strokeWidth: 0 },
			          { cursor: "pointer" },  // indicate that linking may start here
			          new go.Binding("fill", "color")),
							$(go.TextBlock,
			          { margin: 2 },
			          { fromLinkable: false, toLinkable: false },  // don't start drawing a link from the text
			          new go.Binding("text", "name"))),
						$("Button",  // a replacement for "TreeExpanderButton" that works for non-tree-structured graphs
			        // assume initially not visible because there are no links coming out
			        { visible: false },
			        // bind the button visibility to whether it's not a leaf node
			        new go.Binding("visible", "isTreeLeaf",
			                       function(leaf) { return !leaf; })
			            .ofObject(),
			        $(go.Shape,
			          {
			            name: "ButtonIcon",
			            figure: "MinusLine",
			            desiredSize: new go.Size(6, 6)
			          },
			          new go.Binding("figure", "isCollapsed",  // data.isCollapsed remembers "collapsed" or "expanded"
			                         function(collapsed) { return collapsed ? "PlusLine" : "MinusLine"; })),
			        {
			          click: function(e, obj) {
			            e.diagram.startTransaction();
			            var node = obj.part;
			            if (node.data.isCollapsed) {
			              expandFrom(node, node);
			            } else {
			              collapseFrom(node, node);
			            }
			            e.diagram.commitTransaction("toggled visibility of dependencies");
			          }
			        })
			    );

				function collapseFrom(node, start) {
				  if (node.data.isCollapsed) return;
				  node.diagram.model.setDataProperty(node.data, "isCollapsed", true);
				  if (node !== start) node.diagram.model.setDataProperty(node.data, "visible", false);
				  node.findNodesOutOf().each(collapseFrom);
				}

				function expandFrom(node, start) {
				  if (!node.data.isCollapsed) return;
				  node.diagram.model.setDataProperty(node.data, "isCollapsed", false);
				  if (node !== start) node.diagram.model.setDataProperty(node.data, "visible", true);
				  node.findNodesOutOf().each(expandFrom);
				}

				myHistory.linkTemplate =
					$(go.Link,
						{ relinkableFrom: true, relinkableTo: true, corner: 10 },
						$(go.Shape),
						$(go.Shape, { toArrow: "Standard" }));
				 // 监听数据变化更新数据
			     ctrl.$onChanges = function () {
		        	myHistory.model = new go.GraphLinksModel(ctrl.datas.vNodeItemArray,ctrl.datas.vLinkItemArray);
		        }
		        myHistory.addDiagramListener("ObjectSingleClicked", function (e) {
		        	console.log(e.subject.part.data)
		        	ctrl.blur({
		                info: e.subject.part.data
		            });
            	});
			
    }
});
